import axios from "../../http/axios.js";
import {
  apiAddUserServiceAddress,
  apiUpdateUserServiceAddress,
  apiGetUserAddressOne
} from "../../http/server.js";
import areaList from "../../assets/js/area.js";
import {
  regExpSpecificAddress,
  regExpNumber
} from "../../utils/regular.js";

Page({
  data: {
    type: "add", // 判断是进行添加还是编辑
    addressoneIndex: 0, // 服务类目的类型 0 是上门服务 1是院内服务
    selectionAreaList: [], // 选择中的地址数组
    areaList: areaList,
    selectionAreaListCode: "",
    van_pickerShow: false, // 选择器的显示隐藏
    idIndex: 1, // 判断在选择器选之前是点的哪个选项
    columnsArray: [
      "上门服务",
      "院内服务"
    ],
    address: {
      one: {
        id: 1,
        key: "请选择您是上门服务还是院内服务"
      },
      two: {
        id: 2,
        key: "请选择所在的地区"
      },
      three: {
        id: 3,
        key: "请选择所在的医院"
      },
      four: {
        id: 4,
        placeholder: "请填写所在病区",
        value: ""
      },
      five: {
        id: 5,
        placeholder: "请填写所在床号",
        value: ""
      },
      six: {
        id: 6,
        placeholder: "请输入详细地址：如道路、门牌号、小区、楼栋号、单元室等",
        value: ""
      },
      seven: {
        id: 7,
        placeholder: "请输入您所在医院的名字",
        value: ""
      }
    } // 服务地址整体的数据
  },
  onLoad: function(options) {
    let type = options.type;
    this.setData({
      type
    })
    if (type == "add") { //添加地址
      this.initAddressData();
    } else {
      let addressId = JSON.parse(options.item).addressId;
      this.getAddressOne(addressId)
    }
  },
  onReady: function() {},
  onShow: function() {},
  // 初始化地址信息
  initAddressData() {
    this.setData({
      idIndex: 1
    })
  },
  // 获取当前要编辑的服务地址信息
  getAddressOne: function(addressId) {
    axios({
      url: apiGetUserAddressOne,
      data: {
        addressId
      }
    }).then(res => {
      // 根据服务端获取的当前地址进行数据处理
      let address = this.data.address;
      address.one.key = Number(res.type) === 1 ? "上门服务" : "院内服务";
      let selectionAreaListCode = this.data.selectionAreaListCode;
      let addressoneIndex = 0;
      if (Number(res.type) === 1) { // 上门
        address.two.key = res.provinceName === res.cityName ? `${res.provinceName} ${res.countyName}` : `${res.provinceName} ${res.cityName} ${ res.countyName }`
        address.two.provinceCode = res.provinceCode
        address.two.cityCode = res.cityCode
        address.two.countyCode = res.countyCode
        address.two.provinceName = res.provinceName
        address.two.cityName = res.cityName
        address.two.countyName = res.countyName
        address.six.value = res.detailedAddress;
        selectionAreaListCode = res.countyCode;
        addressoneIndex = 0;
      } else { // 院内
        address.seven.value = res.hospital;
        address.four.value = res.inpatientArea;
        address.five.value = res.bed;
        addressoneIndex = 1;
      }
      this.setData({
        address,
        addressId,
        selectionAreaListCode,
        addressoneIndex
      })
    })
  },
  // 校验数据
  check: function() {
    let address = this.data.address;
    let addressoneIndex = this.data.addressoneIndex;
    if (address.one.key == "请选择您是上门服务还是院内服务") {
      wx.showToast({
        title: "请选择您的服务类目",
        icon: "none"
      })
      return false;
    }
    if (addressoneIndex == 0) { // 上门服务
      if (address.two.key == "请选择所在的地区") {
        wx.showToast({
          title: "请选择您的服务地区",
          icon: "none"
        })
        return false;
      }
      if (!address.six.value) {
        wx.showToast({
          title: "请输入您具体的地址信息",
          icon: "none"
        })
        return false;
      }
    } else { // 院内服务
      if (!address.seven.value) {
        wx.showToast({
          title: "请输入您所在医院的名字",
          icon: "none"
        });
        return false;
      }
      if (!address.four.value) {
        wx.showToast({
          title: "请输入您的病区号",
          icon: "none"
        });
        return false;
      }
      if (!regExpNumber.test(address.four.value)) {
        wx.showToast({
          title: "请输入正确的病区号格式",
          icon: "none"
        });
        return false;
      }
      if (!address.five.value) {
        wx.showToast({
          title: "请您输入床位号",
          icon: "none"
        });
        return false;
      }
      if (!regExpNumber.test(address.five.value)) {
        wx.showToast({
          title: "请输入正确的床位号格式",
          icon: "none"
        });
        return false;
      }
    }
    return true;
  },
  //点击保存
  handlClickSuccess: function(e) {
    let test = this.check();
    if (!test) {
      return;
    }
    if (this.data.type == "add") { //添加服务地址
      this.addAddress()
    } else { //编辑服务地址
      this.editAddress();
    }
  },
  commonData() {
    let address = this.data.address;
    let userInfo = wx.getStorageSync("userInfo");
    let data = {};
    // 处理要传送的数据
    if (this.data.addressoneIndex == 0) { //上门服务
      data = {
        // userId: this.data.type === "add" ? userInfo.userId : null,
        userId: userInfo.userId,
        addressId: this.data.type === "add" ? null : this.data.addressId,
        type: address.one.key == "上门服务" ? 1 : 2,
        provinceCode: address.two.provinceCode,
        cityCode: address.two.cityCode,
        countyCode: address.two.countyCode,
        provinceName: address.two.provinceName,
        cityName: address.two.cityName,
        countyName: address.two.countyName,
        detailedAddress: address.six.value
      }
    } else { // 院内服务
      data = {
        // userId: this.data.type === "add" ? userInfo.userId : null,
        userId: userInfo.userId,
        type: address.one.key == "上门服务" ? 1 : 2,
        addressId: this.data.type === "add" ? null : this.data.addressId,
        inpatientArea: address.four.value,
        bed: address.five.value,
        hospital: address.seven.value
      }
    }
    return data;
  },
  //编辑服务地址
  editAddress: function() {
    let putData = this.commonData();
    // console.log("data", putData);
    // return;
    // delete putData.userId;
    axios({
      url: `${apiUpdateUserServiceAddress}/${putData.addressId}`,
      method: "PUT",
      data: putData
    }).then(res => {
      if (res == undefined) {
        wx.showToast({
          title: "服务地址编辑成功",
          icon: "none"
        })
        setTimeout(() => {
          wx.navigateBack();
        }, 1000)
      }
    })
  },
  // 添加服务地址的接口
  addAddress: function() {
    let postData = this.commonData();
    // console.log("传输的数据", postData);
    // return;
    delete postData.addressId;
    axios({
      url: apiAddUserServiceAddress,
      method: "POST",
      data: postData
    }).then(res => {
      wx.showToast({
        title: "服务地址添加成功",
        icon: "none"
      })
      setTimeout(() => {
        wx.navigateBack();
      }, 1500)
    })
    let address = this.data.address;
    let userInfo = wx.getStorageSync("userInfo");
    let data = {};
    // 处理要传送的数据
    if (this.data.addressoneIndex == 0) { //上门服务
      data = {
        userId: userInfo.userId,
        type: address.one.key == "上门服务" ? 1 : 2,
        provinceCode: address.two.provinceCode,
        cityCode: address.two.cityCode,
        countyCode: address.two.countyCode,
        provinceName: address.two.provinceName,
        cityName: address.two.cityName,
        countyName: address.two.countyName,
        detailedAddress: address.six.value
      }
    } else { // 院内服务
      data = {
        userId: userInfo.userId,
        inpatientArea: address.four.value,
        bed: address.five.value,
        hospital: address.seven.value
      }
    }
  },
  // 获取病区床位号
  getInputValue: function(e) {
    let item = e.detail;
    let address = this.data.address;
    switch (Number(item.id)) {
      case 4: // 输入病区
        {
          address.four.value = item.value;
          break
        }
      case 5: // 输入床位号
        {
          address.five.value = item.value;

          break
        }
      case 7: // 输入医院
        {
          address.seven.value = item.value;
          break
        }
      default:
        break
    }
    this.setData({
      address
    })
  },
  // 获取具体详细的地址
  getTextArea: function(e) {
    let value = e.detail.value;
    let address = this.data.address;
    address.six.value = value;
    this.setData({
      address: address
    })
  },
  onClose: function() {
    this.setData({
      van_pickerShow: false
    })
  },
  // 点击弹出框的取消按钮
  onCancel: function() {
    this.setData({
      van_pickerShow: false
    })
  },
  // 点击选择弹出选择器
  handleClickSelect: function(e) {
    let id = e.detail;
    this.setData({
      idIndex: id,
      van_pickerShow: true
    })
  },
  // 选择器点击确认的时候
  onConfirm: function(e) {
    let idIndex = this.data.idIndex;
    let value = e.detail.value;
    let index = e.detail.index;
    let address = this.data.address;
    // 判断从哪个栏目点击弹出的确定框
    switch (Number(idIndex)) {
      case 1: // 选择服务
        {
          address.one.key = value;
          this.setData({
            addressoneIndex: index
          })
          break;
        }
      case 2: // 选择地区
        {
          // console.log(e);
          let values = e.detail.values;
          let key = "";
          if (values[0].name == values[1].name) {
            key = `${values[0].name} ${values[2].name}`
          } else {
            key = `${values[0].name} ${values[1].name} ${values[2].name}`
          }
          address.two.key = key;
          address.two.provinceCode = values[0].code;
          address.two.cityCode = values[1].code;
          address.two.countyCode = values[2].code;
          address.two.provinceName = values[0].name;
          address.two.cityName = values[1].name;
          address.two.countyName = values[2].name;
          break;
        }
      case 3:
        {
          address.three.key = value;
          break;
        }
      default:
        break;
    }
    this.setData({
      address,
      van_pickerShow: false
    })
  }
})